/*
*************************************
* PocketGTT
*************************************
This file is part of PocketGTT.

PocketGTT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

PocketGTT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with PocketGTT.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef GTT_STOP_MANAGER_H
#define GTT_STOP_MANAGER_H

#include <QObject>
#include <QWebPage>
#include <QWebFrame>
#include <QWebElement>
#include <QDeclarativeContext>

#include "gtt_stop.h"
#include "gtt_preferredstoplist.h"


class GTT_StopManager : public QObject
{
    Q_OBJECT

    Q_PROPERTY(msgBanner MgsBannerStatus READ getMgsBannerStatus WRITE setMgsBannerStatus NOTIFY GTTon_MgsBannerStatus_changed )
    Q_PROPERTY(GTT_Stop *StopInfo READ getStopInfo NOTIFY GTTon_StopInfo_changed )
    Q_PROPERTY(QStringList MultiSelectionStop READ getMultiSelectionStop NOTIFY GTTon_MultiSelectionStop_changed )
    Q_PROPERTY(StopPageGUIStatuses StopPageGUIStatus READ getStopPageGUIStatus WRITE setStopPageGUIStatus NOTIFY GTTon_StopPageGUIStatus_changed )
    Q_PROPERTY(SelectMultipleStopsStatuses SelectMultipleStopsStatus READ getSelectMultipleStopsStatus WRITE setSelectMultipleStopsStatus NOTIFY GTTon_SelectMultipleStopsStatus_changed)

    Q_ENUMS(msgBanner)
    Q_ENUMS(StopPageGUIStatuses)
    Q_ENUMS(SelectMultipleStopsStatuses)

public:
    explicit GTT_StopManager(QDeclarativeContext *ctx = 0, QObject *parent = 0);

    enum msgBanner {
        mgsHidden,
        msgLoading,
        mgsNoPassagesIn2h,
        errorNonExistingStop,
        errorUnkownResponse,
        errorDownloadingPage
    };

    enum StopPageGUIStatuses {
        searchPage,
        resultsPage
    };

    enum SelectMultipleStopsStatuses {
        SelectMultipleStopsHidden,
        SelectMultipleStopsVisible
    };

    // PROPERTY StopNumber READ function
    GTT_Stop* getStopInfo();


    // save the list of favorite stops
    GTT_PreferredStopList *myPrefStopList;

    // search by number
    Q_INVOKABLE int searchByString(QString startNum);

    // remove a preferred stop from the list
    Q_INVOKABLE void removePreferred(QString item);

    // update the stop info if requested
    Q_INVOKABLE void reloadStop();

    // understand user selected stop and start download
    Q_INVOKABLE void selectedMultipleStops(int index = -1);

    // programm quit setps
    Q_INVOKABLE int pocketGTTquit();

    // PROPERTY StopPageGUIStatus READ function
    const StopPageGUIStatuses getStopPageGUIStatus();

    // PROPERTY StopPageGUIStatus WRITE function
    void setStopPageGUIStatus(StopPageGUIStatuses status = searchPage);

    // PROPERTY MgsBannerStatus READ function
    const msgBanner getMgsBannerStatus();

    // PROPERTY MgsBannerStatus WRITE function
    void setMgsBannerStatus(msgBanner value);

    // PROPERTY MultiSelectionStop READ function
    const QStringList getMultiSelectionStop();

    // PROPERTY SelectMultipleStopsStatus READ function
    const SelectMultipleStopsStatuses getSelectMultipleStopsStatus();

    // PROPERTY SelectMultipleStopsStatus WRITE function
    void setSelectMultipleStopsStatus(SelectMultipleStopsStatuses newStatus);


signals:
    // downloading info from GTT website signals
    void GTTon_new_stop_changed(const QString &stop);

    // PROPERTY MgsBannerStatus NOTIFY signal
    void GTTon_MgsBannerStatus_changed();

    // PROPERTY StopNumber NOTIFY signal
    void GTTon_StopInfo_changed();

    // PROPERTY StopPageGUIStatus NOTIFY signal
    void GTTon_StopPageGUIStatus_changed();

    // PROPERTY MultiSelectionStop NOTIFY signal
    void GTTon_MultiSelectionStop_changed();

    // PROPERTY SelectMultipleStopsStatus NOTIFY signal
    void GTTon_SelectMultipleStopsStatus_changed();

private:
    // PROPERTY StopNumber
    GTT_Stop *hiddenStopInfo;

    // PROPERTY loading
    msgBanner hiddenMgsBannerStatus;

    // PROPERTY StopTitle
    QString hiddenStopTitle;

    // PROPERTY StopPageGUIStatus
    StopPageGUIStatuses hiddenStopPageGUIStatus;

    // PROPERTY MultiSelectionStop
    QStringList hiddenMultiSelectionStop;

    // PROPERTY SelectMultipleStopsStatus
    SelectMultipleStopsStatuses hiddenSelectMultipleStopsStatuses;

    // manage info from GTT website
    QWebPage mywebpage;
    QWebElement html_doc;

    // manage info from GTT website to get map position
    // (formerly it was done by Google Maps service, so the relative function are still called:
    // GMaps_download_info and GMaps_parse_info
    QWebPage gmwebpage;
    QWebElement gm_html_doc;


    // Save context to push preferred stop collection to QML
    QDeclarativeContext *hiddenCtx;

    // new stop requested from user function download
    void GTT_user_new_stop_def(const QString &stop);


    // extract from page name, number and street of a stop
    void GTT_save_stop_general(QString &info);

    // parse information regarding future passages of lines at the selected stop
    void GTT_parse_lines_schedule_info();

    // download information from GTT website with the GPS position of the stop
    // this function was formerly base on Google Maps service, that's why the name
    void GMaps_download_info(const QString &stop_num, const QString &stop_name);

    // manage multiple stop reply
    void GTT_parseMultipleStops();



private slots:

    // download information from GTT website
    void GTT_download_info(const QString &text);

    // extract stop information from GTT downloaded page
    void GTT_parse_info(bool ok);

    // extract GPS position from GTT downloaded page
    // this function was formerly base on Google Maps service, that's why the name
    void GMaps_parse_info(bool ok);

    // when StopInfo us updated add the info to the preferred list
    void preferredStopListUpdate();


};

#endif // GTT_STOP_MANAGER_H
